# -*- coding: utf-8 -*-
# standard
import os
import sys

# third
from tqdm import tqdm

# local
_P_PATH =  os.path.dirname(os.path.dirname(os.path.realpath(__file__)))
if _P_PATH not in sys.path:
    sys.path.append(_P_PATH)
from functions import *


"""
@Title:   
@File: 检查hk01上的视频文件是否都已存储.py
@Author: walle 2023年12日29日 14时32分34秒
@Version: 1.0.0
@Desc: 
"""

def get_gpu_server_videos():
    """
    获取gpu服务器上的所有视频信息。
    """
    dest_dir = "/mnt/aiface/data/i2v/work_data/"
    command = f'ls -l {dest_dir} | grep "^-"'
    command2 = f'du {dest_dir} -h max-depath=1 --block-size=G'  # 查看文件夹大小，单位G
    ssh = paramiko.SSHClient()  
    ssh.set_missing_host_key_policy(paramiko.AutoAddPolicy())  
    # 连接到远程Linux系统  
    ssh.connect(hostname=SERVER_HOST, port=PORT, username="root", password="Default01!")    
    # 执行命令并获取输出  
    stdin, stdout, stderr = ssh.exec_command(command)  
    output1 = stdout.read().decode()  
    # 执行命令并获取输出  
    stdin, stdout, stderr = ssh.exec_command(command2)  
    output2 = stdout.read().decode() 
    ssh.close()
    if output1 == "":
        good_files, bad_files, size = [], [], 0
    else:
        file_lines = output1.split("\n")
        size = re.match(r"^[0-9,\.]*[a-z,A-Z]", output2).group()
        size = int(size[0: -1])
        print(f"视频总大小： {size}G")
        good_files, bad_files = ScpRecordOrm.format_remote_file_info(file_lines)
    return good_files, bad_files, size


def get_hk01_work_data_videos():
    """
    获取爬虫服务器上的work_data 目录下视频信息。
    """
    list_dir_command = 'ls -l /mnt/projects/work_data/ | grep "^-"'
    ssh = paramiko.SSHClient()  
    ssh.set_missing_host_key_policy(paramiko.AutoAddPolicy())  
    # 连接到远程Linux系统  
    ssh.connect(hostname="47.242.28.132", port=PORT, username="spider", password="jingying")    
    # 执行命令并获取输出  
    stdin, stdout, stderr = ssh.exec_command(list_dir_command)  
    output = stdout.read().decode()  
    ssh.close()
    if output == "":
        good_files, bad_files = [], []
    else:
        file_lines = output.split("\n")
        good_files, bad_files = ScpRecordOrm.format_remote_file_info(file_lines)
    return good_files, bad_files


def main():
    hk1_files = get_hk01_work_data_videos()[0]
    key = "gpu_files"
    data = FileCacheManager.get(key)
    if data is None:
        data = get_gpu_server_videos()
        FileCacheManager.set(key, data, 0)
    gpu_good_files, gpu_bad_files, size = data
    not_exist_files = []
    for file_name in tqdm(hk1_files):
        if file_name in gpu_good_files:
            pass
        elif file_name in gpu_bad_files:
            logger.debug(f"未拷贝完成的文件: {file_name}")
            not_exist_files.append(file_name)
        else:
            logger.debug(f"gpu上不存在的文件: {file_name}")
            not_exist_files.append(file_name)
    logger.debug(f"gpu上不存在的文件数量: {len(not_exist_files)}")
    txt_path = os.path.join(_P_PATH, "test", "videos.txt")
    with open(txt_path, "w", encoding="utf-8") as f:
        f.writelines(not_exist_files)


if __name__ == '__main__':
    main()
    pass